import './style.css'

window.addEventListener('message', (event) => {
  // console.log('App message', event);
  if (event.data === null || event.data === undefined) {
    console.log('App message null or undefined');
    return;
  }

  if (event.data === 'ping') {
    console.log('App message ping');
    (event.source as Window).postMessage('pong', event.origin);
    return;
  }

  console.log('App message data', event.data);
  const data = event.data;
  const { content, preview } = data;

  console.log('auto print data', event.data.content);

  const preview_container = document.querySelector<HTMLDivElement>('#preview')!;
  if (preview === true) {
    preview_container!.innerHTML = content;
    preview_container.style.display = 'block';
  } else {
    preview_container.style.display = 'none';
  }
});
